package com.webchat.common.service;


import com.webchat.common.enums.RedisKeyEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WsSessionRegistryService {

    @Autowired
    private RedisService redisService;

    public void registry(String bizCode, String userId, String instanceHost) {

        String userSessionKey = RedisKeyEnum.USER_WS_SESSION_INSTANCE_CACHE.getKey(bizCode, userId);
        redisService.set(userSessionKey, instanceHost, RedisKeyEnum.USER_WS_SESSION_INSTANCE_CACHE.getExpireTime());
    }

    /**
     * 删除
     *
     * @param userId
     */
    public void remove(String bizCode, String userId) {
        String userSessionKey = RedisKeyEnum.USER_WS_SESSION_INSTANCE_CACHE.getKey(bizCode, userId);
        redisService.remove(userSessionKey);
    }

    /**
     * 查询用户当前ws session所在节点信息
     *
     * @param userId
     * @return
     */
    public String getInstance(String bizCode, String userId) {
        String userSessionKey = RedisKeyEnum.USER_WS_SESSION_INSTANCE_CACHE.getKey(bizCode, userId);
        return redisService.get(userSessionKey);
    }

}
